package com.weme.live.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View

class RoundedSquareProgress : View {

    private val TAG = "RoundedSquareProgress"

    private val maxColor = Color.YELLOW
    private val curColor = Color.GREEN


    private var allLength: Float = 0f
    private val maxProgress = 60
    private var curProgress = 0

    private lateinit var curPaint: Paint
    private lateinit var maxPaint: Paint


    private var width: Int = 0
    private var height: Int = 0
    private var maxProgressWidth: Float = 0f
    private var curProgressWidth: Float = 0f


    private var canDisplayDot = true
    private lateinit var curPath: Path
    private var proWidth: Float = 0f
    private var proHeight: Float = 0f
    private var dotCX: Float = 0f
    private var dotCY: Float = 0f

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView()
    }

    private fun initView() {
        canDisplayDot = true

        curPaint = Paint()
        curProgressWidth = dp2Px(5f)
        curPaint.isAntiAlias = true
        curPaint.style = Paint.Style.STROKE
        curPaint.strokeWidth = curProgressWidth
        curPaint.color = curColor

        maxProgressWidth = dp2Px(5f)
        maxPaint = Paint()
        maxPaint.isAntiAlias = true
        maxPaint.color = maxColor
        maxPaint.style = Paint.Style.STROKE
        maxPaint.strokeWidth = maxProgressWidth


    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        width = measureWidth(widthMeasureSpec)
        height = measureHeight(heightMeasureSpec)
        setMeasuredDimension(width, height)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val tWidth = width - paddingLeft - paddingRight
        val tHeight = height - paddingTop - paddingBottom
        val point1X = paddingLeft + tWidth / 10.toFloat()
        val point1Y = paddingTop + tHeight / 10.toFloat()
        val point2X = tWidth + paddingLeft - tWidth / 10.toFloat()
        val point2Y = paddingTop + tHeight / 10.toFloat()
        val point3X = tWidth + paddingLeft - tWidth / 10 .toFloat()
        val point3Y = tHeight + paddingTop - tHeight / 10.toFloat()
        val point4X = paddingLeft + tWidth / 10.toFloat()
        val point4Y = tHeight + paddingTop - tHeight / 10.toFloat()

        proWidth = point3X - point1X
        proHeight = point3Y - point1Y

//        val maxpath = Path()
//        maxpath.moveTo(point1X, point1Y)
//        maxpath.lineTo(point2X, point2Y)
//        maxpath.lineTo(point3X, point3Y)
//        maxpath.lineTo(point4X, point4Y)
//        maxpath.close()
//        canvas.drawRoundRect(RectF(point1X, point1Y, point3X, point3Y), dp2Px(10f), dp2Px(10f), maxPaint)

        allLength = 2 * (proWidth + proHeight)
        curPath = Path()
        curPath.moveTo(point1X, point1Y)
        val curPersent = curProgress.toFloat() / maxProgress
        if (curPersent > 0) {
            when {
                curPersent < proWidth / allLength -> {
                    dotCX = point1X + allLength * curProgress / maxProgress
                    dotCY = point1Y
                    curPath.lineTo(dotCX, dotCY)
                }
                curPersent < (proHeight + proWidth) / allLength -> {
                    dotCX = point2X
                    dotCY = point1Y + allLength * curProgress / maxProgress - proWidth
                    curPath.lineTo(point2X, point2Y)
                    curPath.lineTo(dotCX, dotCY)
                }
                curPersent < (2 * proWidth + proHeight) / allLength -> {
                    dotCX = point1X + allLength - proHeight - allLength * curProgress / maxProgress
                    dotCY = point4Y
                    curPath.lineTo(point2X, point2Y)
                    curPath.lineTo(point3X, point3Y)
                    curPath.lineTo(dotCX, dotCY)
                }
                curPersent < 1 -> {
                    dotCX = point1X
                    dotCY = point1Y + allLength - allLength * curProgress / maxProgress
                    curPath.lineTo(point2X, point2Y)
                    curPath.lineTo(point3X, point3Y)
                    curPath.lineTo(point4X, point4Y)
                    curPath.lineTo(dotCX, dotCY)
                }
                curPersent > 1 -> {
                    dotCX = point1X
                    dotCY = point1Y
                    curPath.lineTo(point2X, point2Y)
                    curPath.lineTo(point3X, point3Y)
                    curPath.lineTo(point4X, point4Y)
                    curPath.close()
                }
            }
        } else {
            dotCX = point1X
            dotCY = point1Y
            curPath.lineTo(point1X, point1Y)
        }
       // canvas.drawPath(curPath, curPaint)

        val rectF = RectF()
        curPath.computeBounds(rectF, true)

        canvas.drawRoundRect(rectF, 16f, 16f, curPaint)

    }

    private fun measureWidth(widthMeasureSpec: Int): Int {
        val mode = MeasureSpec.getMode(widthMeasureSpec)
        val size = MeasureSpec.getSize(widthMeasureSpec)
        val padding = paddingLeft + paddingRight
        return when (mode) {
            MeasureSpec.EXACTLY -> size
            else -> {
                var result = suggestedMinimumWidth
                result += padding
                if (mode == MeasureSpec.AT_MOST) {
                    result = Math.max(result, size)
                }
                result
            }
        }
    }

    fun setCurProgress(curProgress: Int) {
        this.curProgress = curProgress
        invalidate()
    }

    private fun measureHeight(heightMeasureSpec: Int): Int {
        val mode = MeasureSpec.getMode(heightMeasureSpec)
        val size = MeasureSpec.getSize(heightMeasureSpec)
        val padding = paddingBottom + paddingTop
        return when (mode) {
            MeasureSpec.EXACTLY -> size
            else -> {
                var result = suggestedMinimumHeight
                result += padding
                if (mode == MeasureSpec.AT_MOST) {
                    result = Math.max(result, size)
                }
                result
            }
        }
    }

    private fun dp2Px(dp: Float): Float {
        return dp * context.resources.displayMetrics.density
    }
}
